﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Web;
using Blog.Models;
using BlogModel;

namespace Blog.Data
{
    public class UserRepository
    {
        public static List<UserModel> GetUsers()
        {
            var users =
                from u in DB.Context.UserSet
                where !u.IsDeleted
                select new UserModel
                {
                    UserId = u.UserId,
                    UserName = u.UserName,
                    Password = u.Password,
                    Name = u.Name,
                    BlogsAbout = u.BlogsAbout,
                    Country = u.Country,
                    Presentation = u.Presentation,
                    UserImage = u.UserImage
                };

            return users.ToList();
        }

        public static UserModel GetUserOnUserId(int userId)
        {
            var user = DB.Context.UserSet
                .FirstOrDefault(u => u.UserId == userId && !u.IsDeleted);

            if (user == null)
                return null;

            var userModel = new UserModel
            {
                UserId = user.UserId,
                UserName = user.UserName,
                Password = user.Password,
                Name = user.Name,
                BlogsAbout = user.BlogsAbout,
                Country = user.Country,
                Presentation = user.Presentation,
                UserImage = user.UserImage
            };

            return userModel;
        }

        public static UserModel AuthenticateUser(string userName, string password)
        {
            var user = DB.Context.UserSet.FirstOrDefault(u => u.UserName == userName);
            if (user != null)
            {
                if (user.Password == password)
                {
                    return new UserModel
                    {
                        UserId = user.UserId,
                        UserName = user.UserName,
                        Password = user.Password,
                        Name = user.Name,
                        BlogsAbout = user.BlogsAbout,
                        Country = user.Country,
                        Presentation = user.Presentation,
                        UserImage = user.UserImage                       
                    };
                }
            }
            return null;
        }

        public static int UpdateUser(UserModel userModel)
        {
            var user = new User
            {
                UserId = userModel.UserId,
                UserName = userModel.UserName,
                BlogsAbout = userModel.BlogsAbout,
                Country = userModel.Country,
                Name = userModel.Name,
                Password = userModel.Password,
                Presentation = userModel.Presentation,
                UserImage = userModel.UserImage
            };

            DB.Context.UserSet.AddOrUpdate(u=>u.UserId, user);
            return DB.Context.SaveChanges();
        }
    }
}

